#!/bin/bash
# postinst script for RabbitRemoteControl
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package

INSTALL_ROOT=/opt/RabbitRemoteControl
case "$1" in
    configure)

        if [ ! -f /usr/share/applications/io.github.KangLin.RabbitRemoteControl.desktop ]; then
            if [ -f $INSTALL_ROOT/share/applications/io.github.KangLin.RabbitRemoteControl.desktop ]; then
                ln -s $INSTALL_ROOT/share/applications/io.github.KangLin.RabbitRemoteControl.desktop /usr/share/applications/io.github.KangLin.RabbitRemoteControl.desktop
            fi
        fi
        if [ ! -f /usr/share/pixmaps/io.github.KangLin.RabbitRemoteControl.svg ]; then
            if [ -f $INSTALL_ROOT/share/icons/hicolor/scalable/apps/io.github.KangLin.RabbitRemoteControl.svg ]; then
                if [ ! -d /usr/share/pixmaps ]; then
                    mkdir -p /usr/share/pixmaps
                fi
                ln -s $INSTALL_ROOT/share/icons/hicolor/scalable/apps/io.github.KangLin.RabbitRemoteControl.svg /usr/share/pixmaps/io.github.KangLin.RabbitRemoteControl.svg
            fi
        fi
        
        if [ ! -f /usr/share/applications/io.github.KangLin.RabbitRemoteControl.Service.Configure.desktop ]; then
            if [ -f $INSTALL_ROOT/share/applications/io.github.KangLin.RabbitRemoteControl.Service.Configure.desktop ]; then
                ln -s $INSTALL_ROOT/share/applications/io.github.KangLin.RabbitRemoteControl.Service.Configure.desktop /usr/share/applications/io.github.KangLin.RabbitRemoteControl.Service.Configure.desktop
            fi
        fi

        if [ ! -f /usr/share/pixmaps/io.github.KangLin.RabbitRemoteControl.Service.Configure.svg ]; then
            if [ -f $INSTALL_ROOT/share/icons/hicolor/scalable/apps/io.github.KangLin.RabbitRemoteControl.Service.Configure.svg ]; then
                if [ ! -d /usr/share/pixmaps ]; then
                    mkdir -p /usr/share/pixmaps
                fi
                ln -s $INSTALL_ROOT/share/icons/hicolor/scalable/apps/io.github.KangLin.RabbitRemoteControl.Service.Configure.svg /usr/share/pixmaps/io.github.KangLin.RabbitRemoteControl.Service.Configure.svg
            fi
        fi
        
        if [ ! -f /usr/bin/rabbitremotecontrol ]; then
            ln -s $INSTALL_ROOT/bin/RabbitRemoteControlApp /usr/bin/rabbitremotecontrol
        fi

        if [ -d $INSTALL_ROOT/etc ]; then
            chmod -R a+rw $INSTALL_ROOT/etc
        fi

        #echo "$INSTALL_ROOT/lib" > /etc/ld.so.conf.d/RabbitRemoteControl.conf
        #echo "$INSTALL_ROOT/lib/`uname -m`-linux-gnu" >> /etc/ld.so.conf.d/RabbitRemoteControl.conf
        #echo "$INSTALL_ROOT/bin" >> /etc/ld.so.conf.d/RabbitRemoteControl.conf
        #export QT_VERSION_DIR=
        #if [ -n "${QT_VERSION_DIR}" ]; then
        #    echo "/opt/qt${QT_VERSION_DIR}/lib" >> /etc/ld.so.conf.d/RabbitRemoteControl.conf
        #fi
        #ldconfig
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
        #rm -fr /etc/ld.so.conf.d/RabbitRemoteControl.conf
        rm -fr /usr/share/applications/io.github.KangLin.RabbitRemoteControl.desktop
        rm -fr /usr/share/pixmaps/io.github.KangLin.RabbitRemoteControl.svg
        rm -fr /usr/share/applications/io.github.KangLin.RabbitRemoteControl.Service.Configure.desktop
        rm -fr /usr/share/pixmaps/io.github.KangLin.RabbitRemoteControl.Service.Configure.svg
        ldconfig
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
